from rest_framework import serializers
from rest_framework.serializers import raise_errors_on_nested_writes
from rest_framework.utils import model_meta
from party.utils.qiniuyun.qiniu_storage import storage
from answers.models import Label, Question, Reply
from articles.models import Article
from recruits.models import Enterprise
from users.models import User
import base64



class ModifyPersonInfomationEnterpiseUserSerializers(serializers.ModelSerializer):

    recruits = serializers.PrimaryKeyRelatedField(read_only=True,many=True)

    class Meta:

        model = Enterprise

        fields = ('id', 'name', 'labels', 'logo', 'recruits', 'summary')



class ModifyPersonInfomationArticleInnerArticleSerializers(serializers.ModelSerializer):

    class Meta:

        model = Article

        fields = ('id','title')


class ModifyPersonInfomationArticleInnerUserSerializers(serializers.ModelSerializer):

    articles = ModifyPersonInfomationArticleInnerArticleSerializers(read_only=True,many=True)

    class Meta:

        model = User

        fields = ('id', 'username', 'avatar', 'articles', 'fans')



class ModifyPersonInfomationOuterCollectedArticleSerializers(serializers.ModelSerializer):

    user =ModifyPersonInfomationArticleInnerUserSerializers(read_only=True,many=True)
    collected = serializers.BooleanField(default=False)

    class Meta:

        model = Article

        fields = ('id', 'title', 'content','createtime','user','collected_users','collected','image')


class ModifyPersonInfomationUserSerializers(serializers.ModelSerializer):

    class Meta:

        model = User

        fields = ('id', 'username', 'avatar')


class ModifyPersonInfomationSubsSerializers(serializers.ModelSerializer):

    user = serializers.StringRelatedField(label='labels', read_only=True)

    class Meta:

        model = Reply

        fields = ('id', 'content', 'createtime','useful_count','unuseful_count','user')


class ModifyOuterAnswerQuestionSerializers(serializers.ModelSerializer):

    user = serializers.StringRelatedField(label='labels', read_only=True)
    subs = ModifyPersonInfomationSubsSerializers(read_only=True,many=True)

    class Meta:

        model = Reply

        fields = ('id', 'content', 'createtime', 'useful_count', 'problem', 'unuseful_count',
                  'subs', 'user', 'parent')


class ModifyOuterQuestionSerializers(serializers.ModelSerializer):

    labels = serializers.StringRelatedField(label='labels',read_only=True)

    class Meta:

        model = Question

        fields = ('id','createtime','labels','reply','replyname','replytime',
                  'title','unuseful_count','useful_count','user','visits')



class ModifyOuterLabelSerializers(serializers.ModelSerializer):


    class Meta:

        model = Label
        fields = ('id','label_name')


class ModifyPersonInfomationViewSerializers(serializers.ModelSerializer):

    labels = ModifyOuterLabelSerializers(read_only=True,many=True)
    questions = ModifyOuterQuestionSerializers(read_only=True,many=True)
    answer_question = ModifyOuterAnswerQuestionSerializers(read_only=True,many=True)
    collected_articles = ModifyPersonInfomationOuterCollectedArticleSerializers(read_only=True,many=True)
    enterpises = ModifyPersonInfomationEnterpiseUserSerializers(read_only=True,many=True)
    avatar = serializers.CharField()

    class Meta:

        model = User

        fields = ('id','username','mobile','realname','birthday','sex',
                  'avatar','website','email','city','address',
                  'labels','questions','answer_question','collected_articles','enterpises')


class AddSkillLabelSerializers(serializers.ModelSerializer):

    labels = serializers.PrimaryKeyRelatedField(required=True, many=True,queryset=Label.objects.all())

    class Meta:

        model = User

        fields = ('id','labels')